############################################################## 
## MOD Title: Gold, Bronze, Silver statuses of downloads 
## MOD Author: Lektor < lektor@normaplus.com > http://www.phpbbguru.net/community/member5373.html
## MOD Description: This mod  makes it possible to set the topic's type of download.
## MOD Version: 0.7.1 
## 
## Installation Level: (Easy) 
## Installation Time: 15 Minutes 
## Files To Edit:  posting.php
##                 viewforum.php
##		   tracker.php
##		   search.php
##		   groupcp.php
##		   bt/includes/tr_announcer.php
##	   	   includes/auth.php
##		   includes/functions_search.php
##		   includes/functions_post.php
##		   includes/functions.php
##		   includes/constants.php
##		   attach_mod/displaying_torrent.php
##		   templates/subSilver/posting_body.tpl
##		   templates/SubSilver/viewforum_torrent.tpl
##		   templates/SubSilver/viewtopic_attach_body.tpl
##		   templates/SubSilver/tracker.tpl
##		   templates/SubSilver/search_username.tpl
##
## Included Files: admin/admin_dl_statuses.php 
##		   images/gold.gif 
##		   images/silver.gif 
##		   images/bronze.gif 
##                 templates/subSilver/admin/admin_dl_statuses.tpl
############################################################## 
## For Security Purposes, Please Check: http://www.phpbbguru.net/mods/ for the 
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code 
## to enter into your phpBB Forum. 
############################################################## 
## Author Notes: n/a
############################################################## 
## MOD History: 
## 
##   2008-05-28 - Version 0.1.0 
##      - Initial Release
##   2008-06-03 - Version 0.5.0 
##      - added search on tracker by statuses of downloads 
##      - added an admin module where you can set the privileges for some users and groups
##      - added an image of download's status in viewtopic page (in attachments table) and in the tracker search page (in the table of search results)
##   2008-06-13 - Version 0.7.0 
##      - added restriction system
##   2008-06-13 - Version 0.7.1 
##      - fixed some small bugs
## 
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
############################################################## 
#
#-----[ SQL ]------------------------------------------
#
ALTER TABLE `phpbb_topics` ADD `topic_type_gold` TINYINT( 3 ) UNSIGNED DEFAULT '0' NOT NULL ;

INSERT INTO `phpbb_config` VALUES ('dl_statuses_perms', 'auth_admin');

CREATE TABLE `phpbb_dl_st_prevs_list` (
  `prevs_id` mediumint(8) unsigned NOT NULL auto_increment,
  `prevs_user_pg` mediumint(8) NOT NULL default '0',
  `prevs_user_png` mediumint(8) NOT NULL default '0',
  `prevs_group_g` mediumint(8) NOT NULL default '0',
  `prevs_until` int(11) NOT NULL default '0',
  PRIMARY KEY  (`prevs_id`),
  KEY `prevs_user_pg_user_png_group_g` (`prevs_user_pg`,`prevs_user_png`,`prevs_group_g`)
);
ALTER TABLE `phpbb_users` ADD `r_total_q` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_total_d` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_per_p` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_period_t` int(11)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_next_t` int(11)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_cur_c` mediumint(8)  DEFAULT '0' NOT NULL ;

ALTER TABLE `phpbb_groups` ADD `r_total_q` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_groups` ADD `r_per_p` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_groups` ADD `r_period_t` int(11)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_groups` ADD `r_combin_k` mediumint(8)  DEFAULT '0' NOT NULL ;

#
#-----[ COPY ]------------------------------------------
#
copy root/admin/admin_dl_statuses.php to admin/admin_dl_statuses.php
copy root/templates/subSilver/admin/admin_dl_statuses.tpl to templates/subSilver/admin/admin_dl_statuses.tpl
copy root/images/*.* to images/*.*


#
#-----[ OPEN ]------------------------------------------
#
posting.php

#
#-----[ FIND ]------------------------------------------
#
//
// End session management
//

#
#-----[ AFTER, ADD ]------------------------------------------
#
prune_dl_st_prevs_list();
refresh_restriction();

#
#-----[ FIND ]------------------------------------------
#
//
// Set toggles for various options
//

#
#-----[ BEFORE, ADD ]------------------------------------------
#
if ( $post_info['allow_reg_tracker'] )
{
   $r_data = explode("::", check_prevs_g($userdata['user_id']));
   $access= $r_data[0];
   $your_perms=$r_data[1];
   $blacklist= check_prevs_png($userdata['user_id']);
}

#
#-----[ FIND ]------------------------------------------
#
//
// Set topic type
//
$topic_type = ( !empty($HTTP_POST_VARS['topictype']) ) ? intval($HTTP_POST_VARS['topictype']) : POST_NORMAL;
$topic_type = ( in_array($topic_type, array(POST_NORMAL, POST_STICKY, POST_ANNOUNCE)) ) ? $topic_type : POST_NORMAL;

#
#-----[ AFTER, ADD ]------------------------------------------
#
$topic_type_gold = ( !empty($HTTP_POST_VARS['topictypegold']) ) ? intval($HTTP_POST_VARS['topictypegold']) : 0;
$topic_type_gold = ( in_array($topic_type_gold, array(0, 1, 2, 3)) ) ? $topic_type_gold : 0;


#
#-----[ FIND ]------------------------------------------
#
		$sql = "SELECT f.*, t.topic_id, t.topic_status, t.topic_type,		

#
#-----[ IN-LINE FIND ]------------------------------------------
#
, t.topic_type

#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
, t.topic_type_gold

#
#-----[ FIND ]------------------------------------------
#
		$post_data['topic_type'] = $post_info['topic_type'];
		
#
#-----[ AFTER, ADD ]------------------------------------------
#
		$post_data['topic_type_gold'] = $post_info['topic_type_gold'];
#
#-----[ FIND ]------------------------------------------
#
		$post_data['edit_poll'] = false;
		
#
#-----[ AFTER, ADD ]------------------------------------------
#
		$post_data['topic_type_gold'] = 0;
		
#
#-----[ FIND ]------------------------------------------
#
				$topic_type = ( $topic_type != $post_data['topic_type'] && !$is_auth['auth_sticky'] && !$is_auth['auth_announce'] ) ? $post_data['topic_type'] : $topic_type;

#
#-----[ AFTER, ADD ]------------------------------------------
#
            $topic_type_gold = ($post_info['allow_reg_tracker'])? ( (($topic_type_gold != $post_data['topic_type_gold']) && ($access && !$blacklist) ) ? $topic_type_gold : $post_data['topic_type_gold']) : ($post_data['topic_type_gold']);

#
#-----[ FIND ]------------------------------------------
#
				submit_post(

#
#-----[ IN-LINE FIND ]------------------------------------------
#
, $topic_type

#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
, $topic_type_gold
				
#
#-----[ FIND ]------------------------------------------
#
	if ( $topic_type_toggle != '' )
	{
		$topic_type_toggle = $lang['Post_topic_as'] . ': <input type="radio" name="topictype" value="' . POST_NORMAL .'"' . ( ( $post_data['topic_type'] == POST_NORMAL || $topic_type == POST_NORMAL ) ? ' checked="checked"' : '' ) . ' /> ' . $lang['Post_Normal'] . '&nbsp;&nbsp;' . $topic_type_toggle;
	}
}

#
#-----[ AFTER, ADD ]------------------------------------------
#
$s_type_gold = '';
if ( $mode == 'newtopic' || ( $mode == 'editpost' && $post_data['first_post'] ) )
{
	if ( $access && !$blacklist && $post_info['allow_reg_tracker'] )
	{
		$template->assign_block_vars('switch_type_gold', array());

		$s_type_gold .= '<input type="radio" name="topictypegold" value="1"';
		if ( $post_data['topic_type_gold'] == 1 || $topic_type_gold == 1 )
		{
			$s_type_gold .= ' checked="checked"';
		}
		$s_type_gold .= ' /> ' . '' . '&nbsp;&nbsp;';

		$s_type_gold .= '<input type="radio" name="topictypegold" value="2"';
		if ( $post_data['topic_type_gold'] == 2 || $topic_type_gold == 2 )
		{
			$s_type_gold .= ' checked="checked"';
		}
		$s_type_gold .= ' /> ' . '' . '&nbsp;&nbsp;';
	
		$s_type_gold .= '<input type="radio" name="topictypegold" value="3"';
		if ( $post_data['topic_type_gold'] == 3 || $topic_type_gold == 3 )
		{
			$s_type_gold .= ' checked="checked"';
		}
		$s_type_gold .= ' /> ' . '' . '&nbsp;&nbsp;';
	}
	if ( $s_type_gold != '' )
	{
		$s_type_gold = '<input type="radio" name="topictypegold" value="0"' . ( ( $post_data['topic_type_gold'] == 0 || $topic_type_gold == 0 ) ? ' checked="checked"' : '' ) . ' /> ' . '' . '&nbsp;&nbsp;' . $s_type_gold;
	}
}


#
#-----[ FIND ]------------------------------------------
#
	'S_TYPE_TOGGLE' => $topic_type_toggle,
	
#
#-----[ AFTER, ADD ]------------------------------------------
#
	'S_TYPE_GOLD' => $s_type_gold,
	'YOUR_PERMS' => $your_perms,
	
#
#-----[ OPEN ]------------------------------------------
#
includes/functions_post.php

#
#-----[ FIND ]------------------------------------------
#
function submit_post(

#
#-----[ IN-LINE FIND ]------------------------------------------
#
, &$topic_type

#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
, &$topic_type_gold

#
#-----[ FIND ]------------------------------------------
#
  	$sql  = ($mode != "editpost") ? "INSERT INTO " . TOPICS_TABLE . " (

#
#-----[ IN-LINE FIND ]------------------------------------------
#
, topic_type

#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
, topic_type_gold

#
#-----[ IN-LINE FIND ]------------------------------------------
#
, $topic_type

#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
, $topic_type_gold

#
#-----[ IN-LINE FIND ]------------------------------------------
#
, topic_type = $topic_type

#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
, topic_type_gold = '$topic_type_gold'

#
#-----[ FIND ]------------------------------------------
#
	add_search_words('single', $post_id, stripslashes($post_message), stripslashes($post_subject));

#
#-----[ AFTER ADD ]------------------------------------------
#
	if ($topic_type_gold >0)
		{
				$user_id=$userdata['user_id'];
				$curent_time=time();
				$sql_r_total_q = ($userdata['r_total_q'] == '-1')? '': 'r_total_q=r_total_q-1,';
				$sql_r_cur_c = ($userdata['r_per_p'] < '1')? '': 'r_cur_c=r_cur_c+1,';
				$sql_r_next_t =($userdata['r_per_p'] < '1')? '': ($userdata['r_cur_c']==0)? 'r_next_t='.$curent_time.'+(r_period_t*86400),':'';

				$sql = "UPDATE " . USERS_TABLE . " SET $sql_r_total_q $sql_r_cur_c $sql_r_next_t r_total_d=r_total_d+1 WHERE user_id=$user_id";
				if (!$db->sql_query($sql))
					{
						message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
					}

		}

#
#-----[ OPEN ]------------------------------------------
#
templates/subSilver/posting_body.tpl

#
#-----[ FIND ]------------------------------------------
#
		  <!-- BEGIN switch_type_toggle -->
		  <tr> 
			<td></td>
			<td><span class="gen">{S_TYPE_TOGGLE}</span></td>
		  </tr>
		  <!-- END switch_type_toggle -->
		  
#
#-----[ AFTER, ADD ]------------------------------------------
#
		  <!-- BEGIN switch_type_gold -->
		<fieldset class="fieldset" style="margin: 2px;">
			<legend> </legend>
			<div style="padding: 3px">
			 {S_TYPE_GOLD}
			</div>
			<div style="background-color: #DCE3F5;border: 1px dotted black;">{YOUR_PERMS}</div>
		</fieldset>
		  <!-- END switch_type_gold -->
		  
#
#-----[ OPEN ]------------------------------------------
#
bt/includes/tr_announcer.php

# 
#-----[ FIND ]--------------------------------------------- 
# 
      $sql = 'SELECT t.torrent_id, t.attach_id, t.topic_id, t.poster_id, t.size, t.seeder_last_seen, u.user_id, u.u_up_total, u.u_down_total 
      FROM '. BT_TORRENTS_TABLE .' t 
      LEFT JOIN '. BT_USERS_TABLE ." u ON u.auth_key = '$auth_key_sql' 
      WHERE t.info_hash = '$info_hash_sql' 
      LIMIT 1"; 

# 
#-----[ REPLACE WITH ]--------------------------------------------- 
# 
   $sql = 'SELECT t.torrent_id, t.attach_id, t.topic_id, t.poster_id, t.size, t.seeder_last_seen, u.user_id, u.u_up_total, u.u_down_total, topics.topic_type_gold 
      FROM '. BT_TORRENTS_TABLE .' t, phpbb_topics topics 
      LEFT JOIN '. BT_USERS_TABLE ." u ON u.auth_key = '$auth_key_sql' 
      WHERE t.info_hash = '$info_hash_sql' 
      AND topics.topic_id = t.topic_id 
      LIMIT 1"; 

# 
#-----[ FIND ]--------------------------------------------- 
# 
$s_last_seen = $row['seeder_last_seen'];

# 
#-----[ AFTER ADD ]--------------------------------------------- 
# 
$topic_type_gold = $row['topic_type_gold'];

# 
#-----[ FIND ]--------------------------------------------- 
# 
		update_user_tor_up_down_stat($torrent_id, $user_id, $up_add, $down_add);

# 
#-----[ AFTER ADD ]--------------------------------------------- 
# 
		if ($down_add) {
			switch ($topic_type_gold)
			{
				case 1:
				$down_add=0;
				break;
				case 2:
				$down_add=$down_add / 3;
				break;
				case 3: 
				$down_add=$down_add / 2;
				break;
			}
		
		}

# 
#-----[ OPEN ]--------------------------------------------- 
# 
viewforum.php

# 
#-----[ FIND ]---------------------------------------------
# 
		if( $topic_rowset[$i]['topic_vote'] )

# 
#-----[ BEFORE, ADD ]---------------------------------------
# 
		switch ($topic_rowset[$i]['topic_type_gold'])
			{
			case 1: $topic_type_gold='<img src="images/gold.gif"> '; break; 
			case 2: $topic_type_gold='<img src="images/silver.gif"> '; break; 
			case 3: $topic_type_gold='<img src="images/bronze.gif"> '; break; 
			default: $topic_type_gold='';
			}
# 
#-----[ FIND ]---------------------------------------------
# 
         'TOPIC_TYPE' => $topic_type,

# 
#-----[ AFTER, ADD ]---------------------------------------
# 
         'TOPIC_TYPE_GOLD' => $topic_type_gold,

# 
#-----[ OPEN ]--------------------------------------------- 
# 
templates/SubSilver/viewforum_torrent.tpl

# 
#-----[ FIND ]---------------------------------------------
# 
<a href="{topicrow.U_VIEW_TOPIC}">{topicrow.TOPIC_TITLE}</a>

# 
#-----[ IN-LINE FIND ]---------------------------------------
# 
<a href="{topicrow.U_VIEW_TOPIC}


# 
#-----[ IN-LINE BEFORE, ADD ]---------------------------------------
# 
{topicrow.TOPIC_TYPE_GOLD} 

#
#-----[ OPEN ]------------------------------------------
#
includes/constants.php

#
#-----[ FIND ]------------------------------------------
#
// Table names

#
#-----[ AFTER, ADD ]------------------------------------------
#
define('DOWNLOAD_STATUSES_TBL', $table_prefix.'dl_st_prevs_list');


#
#-----[ OPEN ]------------------------------------------
#
includes/functions.php

#
#-----[ FIND ]------------------------------------------
#
?>

#
#-----[ BEFORE, ADD ]------------------------------------------
#
function get_groupdata($group, $force_str = false)
{
	global $db;

	if (!is_numeric($group) || $force_str)
	{
		$group = phpbb_clean_username($group);
	}
	else
	{
		$group = intval($group);
	}

	$sql = "SELECT *
		FROM " . GROUPS_TABLE . " 
		WHERE ";
	$sql .= ( ( is_integer($group) ) ? "group_id = $group" : "group_name = '" .  str_replace("\'", "''", $group) . "'" ) . " AND group_id <> 0";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Tried obtaining data for a non-existent group', '', __LINE__, __FILE__, $sql);
	}

	// Start Advanced IP Tools Pack MOD
	if ( $db->sql_affectedrows() == 0 )
	{
		message_die(GENERAL_ERROR, 'Group does not exist.');
	}
	// End Advanced IP Tools Pack MOD

	return ( $row = $db->sql_fetchrow($result) ) ? $row : false;
}

function prune_dl_st_prevs_list()
{
	global $db;

	$sql = "DELETE FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_until <> 0 AND prevs_until < " . time();
	if (!($result = $db->sql_query($sql)))
	{
		message_die(GENERAL_ERROR, 'Could not access prevslist', '', __LINE__, __FILE__, $sql);
	}

}

function refresh_restriction()
{
	global $db, $userdata;
	$current_time=time();
	$user_id = $userdata['user_id'];

	$sql = "UPDATE phpbb_users SET r_cur_c=0, r_next_t=0 WHERE r_next_t!=0 AND r_next_t<$current_time AND user_id=$user_id";
		if (!($result = $db->sql_query($sql)))
		{
			message_die(GENERAL_ERROR, 'Could not ..', '', __LINE__, __FILE__, $sql);
		}
}


function check_prevs_g($user_id)
{
	global $db, $userdata, $is_auth, $board_config;

	$acces_key=false;
	$in_p_group=false;
	$your_perm='';

	$sql_in = ''; 
	$sql = "select group_id
		from ".USER_GROUP_TABLE."
		where user_id=$user_id
		and user_pending=0";
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result) )
	{
		$sql_in .= ( ( $sql_in != '' ) ? ', ' : '' ) . $row['group_id'];
	}
	$db->sql_freeresult($result);
	if ($sql_in!= '')
	{
		$sql = "SELECT * 
			FROM " . DOWNLOAD_STATUSES_TBL . "
			WHERE prevs_group_g IN ($sql_in)";
		if (!($result = $db->sql_query($sql)))
		{
			message_die(GENERAL_ERROR, 'Could not access perm list', '', __LINE__, __FILE__, $sql);
		}
		if ($db->sql_fetchrow($result))
		{
			$in_p_group=true;
		}
		$db->sql_freeresult($result);
	}

	$sql = "SELECT * 
		FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_user_pg =$user_id";
	if (!($result = $db->sql_query($sql))) message_die(GENERAL_ERROR, 'Could not access perm list', '', __LINE__, __FILE__, $sql);

	if ($db->sql_fetchrow($result) || $is_auth[$board_config['dl_statuses_perms']] || $in_p_group)
		{
			if (!$is_auth['auth_admin']) 
			{
				$your_perm = ($userdata['r_total_q']=='-1') ? '<span style="color:blue;font-weight:bold;">  -</span>': '<span style="color:blue;font-weight:bold;">'.$userdata['r_total_q'].'</span>';
				$your_perm = '      '.$your_perm.' <br />';
				$your_perm .= ($userdata['r_per_p']=='-1')? '': '  <span style="color:blue;font-weight:bold;">'. $userdata['r_per_p'].'</span>   <span style="color:blue;font-weight:bold;">'.$userdata['r_period_t'].'</span> <br />' ;

				$your_perm .= ($userdata['r_next_t']=='0')? (''): (($userdata['r_cur_c']<$userdata['r_per_p'])? ('    <span style="color:blue;font-weight:bold;">'.($userdata['r_per_p']-$userdata['r_cur_c']).'</span>'.'   <b>'.create_date('d.m.Y g:i:s', $userdata['r_next_t'], $board_config['board_timezone']).'</b><br>'):('      <b>'.create_date('d.m.Y g:i:s', $userdata['r_next_t'], $board_config['board_timezone']).'</b><br>'));

			}
			if (($userdata['r_total_q']!='0' || $userdata['r_total_q']=='-1') && (($userdata['r_cur_c'] < $userdata['r_per_p'] && ($userdata['r_per_p']>0 && $userdata['r_period_t']>0 )) || $userdata['r_per_p']=='-1') || $is_auth['auth_admin'])
			{
				$acces_key=true;
			}
		}					

	return $r_data=$acces_key.'::'.$your_perm;
}

function check_prevs_png($user_id)
{
	global $db;

	$sql = "SELECT * 
		FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_user_png =$user_id";
	if (!($result = $db->sql_query($sql))) message_die(GENERAL_ERROR, 'Could not access perm list', '', __LINE__, __FILE__, $sql);

	if ($db->sql_fetchrow($result))	{
		return true;		
	}else{ return false; }
}

#
#-----[ OPEN ]------------------------------------------
#
attach_mod/displaying_torrent.php

#
#-----[ FIND ]------------------------------------------
#
	$template->assign_block_vars('postrow.attach.tor_reged', array(

#
#-----[ BEFORE, ADD ]------------------------------------------
#
	 switch ($forum_topic_data['topic_type_gold'])
	         {
	         case 1: $topic_type_gold='<img src="images/gold.gif" alt=" "> '; break; 
	         case 2: $topic_type_gold='<img src="images/silver.gif" alt=" "> '; break; 
	         case 3: $topic_type_gold='<img src="images/bronze.gif" alt=" "> '; break; 
	         default: $topic_type_gold='';
	         }

#
#-----[ FIND ]------------------------------------------
#
  		'S_UPLOAD_IMAGE'      => $upload_image,

#
#-----[ AFTER, ADD ]------------------------------------------
#
		'TOPIC_TYPE_GOLD'     => $topic_type_gold,

#
#-----[ OPEN ]------------------------------------------
#
templates/SubSilver/viewtopic_attach_body.tpl

#
#-----[ FIND ]------------------------------------------
#
<td width="10%" class="attachrow" rowspan="6" align="center" style="padding: 5px" nowrap="nowrap">{postrow.attach.tor_reged.S_UPLOAD_IMAGE}<b>{postrow.attach.tor_reged.L_DOWNLOADEDT}</b>

#
#-----[ IN-LINE FIND ]------------------------------------------
#
{postrow.attach.tor_reged.FILESIZE}

#
#-----[ IN-LINE AFTER ADD ]------------------------------------------
#
<br /><br />{postrow.attach.tor_reged.TOPIC_TYPE_GOLD}


#
#-----[ OPEN ]------------------------------------------
#
tracker.php

#
#-----[ FIND ]------------------------------------------
#
$tor_colspan         = 13;   // colspan with all columns

#
#-----[ REPLACE WITH ]------------------------------------------
#
$tor_colspan         = 14;   // colspan with all columns

#
#-----[ FIND ]------------------------------------------
#
$tor_list_sql = $tor_to_show_sql = $opt = $order_options = $time_options = $s_not_seen_options = '';

#
#-----[ REPLACE WITH ]------------------------------------------
#
$tor_list_sql = $tor_to_show_sql = $opt = $order_options = $time_options = $s_not_seen_options = $s_dl_status_options = '';

#
#-----[ FIND ]------------------------------------------
#
$s_not_seen_key  = 'sns';

#
#-----[ AFTER, ADD ]------------------------------------------
#
$s_dl_status_key  = 'sds';

#
#-----[ FIND ]------------------------------------------
#
// Seeder not seen
$s_not_seen_opt = array(
        $search_all => array(
                     'lang' => $lang['Bt_Disregard'],
                     'sql'  => 0
                    ),
        $never  => array(
                     'lang' => $lang['Bt_Never'],
                     'sql'  => 0
                    ),
        30 => array(
                     'lang' => $lang['Bt_1_Month'],
                     'sql'  => $current_time - 86400*30
                    ),
        14 => array(
                     'lang' => $lang['Bt_2_Weeks'],
                     'sql'  => $current_time - 86400*14
                    ),
        7  => array(
                     'lang' => $lang['Bt_7_Days'],
                     'sql'  => $current_time - 86400*7
                    ),
        3  => array(
                     'lang' => $lang['Bt_3_Days'],
                     'sql'  => $current_time - 86400*3
                    )
);

#
#-----[ AFTER, ADD ]------------------------------------------
#
// Download statuses
$s_dl_status_opt = array(
        $search_all => array(
                     'lang' => '  ',
                     'sql'  => 0
                    ),
        0 => array(
                     'lang' => ' ',
                     'sql'  => 0
                    ),
        1 => array(
                     'lang' => ' ',
                     'sql'  => 1
                    ),
        2  => array(
                     'lang' => ' ',
                     'sql'  => 2
                    ),
        3  => array(
                     'lang' => ' ',
                     'sql'  => 3
                    )
);

#
#-----[ FIND ]------------------------------------------
#
$s_not_seen_val  = $def_s_not_seen_val  = $search_all;

#
#-----[ AFTER, ADD ]------------------------------------------
#
$s_dl_status_val  = $def_s_dl_status_val  = $search_all;


#
#-----[ FIND ]------------------------------------------
#
	select_get_val ($s_not_seen_key, $s_not_seen_val,  $s_not_seen_opt,  $def_s_not_seen_val);

#
#-----[ AFTER, ADD ]------------------------------------------
#
        select_get_val ($s_dl_status_key, $s_dl_status_val,  $s_dl_status_opt,  $def_s_dl_status_val);

#
#-----[ FIND ]------------------------------------------
#
		$s_not_seen_key  => $s_not_seen_val,
#
#-----[ AFTER, ADD ]------------------------------------------
#
                $s_dl_status_key  => $s_dl_status_val,

#
#-----[ FIND ]------------------------------------------
#
	$s_not_seen  = ($s_not_seen_val != $search_all) ? TRUE : FALSE;

#
#-----[ AFTER, ADD ]------------------------------------------
#
        $s_dl_status  = ($s_dl_status_val != $search_all) ? TRUE : FALSE;

#
#-----[ FIND ]------------------------------------------
#
                $s_seen_exl  = ($s_not_seen_val == $never) ? '' : 'AND tor.seeder_last_seen != 0';

#
#-----[ AFTER, ADD ]------------------------------------------
#
                $s_dl_status_v = $s_dl_status_opt[$s_dl_status_val]['sql'];

#
#-----[ FIND ]------------------------------------------
#
		$where_sql .= ($s_not_seen) ? "
				AND tor.seeder_last_seen $s_seen_sign $s_seen_time
				$s_seen_exl
		" :	'';

#
#-----[ AFTER, ADD ]------------------------------------------
#
                $where_sql .= ($s_dl_status) ? "
                                AND t.topic_type_gold=$s_dl_status_v
                " :     '';

#
#-----[ FIND ]------------------------------------------
#
				$compl  = $tor_row['complete_count'] + $tor_row['complete_count_guest'];

#
#-----[ AFTER, ADD ]------------------------------------------
#
                                 switch ($tor_row['topic_type_gold'])
                                         {
                                         case 1: $topic_type_gold='<img src="images/gold.gif"> '; $l_status=' '; break; 
                                         case 2: $topic_type_gold='<img src="images/silver.gif"> '; $l_status=' '; break; 
                                         case 3: $topic_type_gold='<img src="images/bronze.gif"> '; $l_status='  '; break; 
                                         default: $topic_type_gold='<span style=color:white;> - <span>'; $l_status=' '; break; 
                                         }

#
#-----[ FIND ]------------------------------------------
#
					'COMPLETED'    => ($compl) ? $compl : 0,
#
#-----[ AFTER, ADD ]------------------------------------------
#
                                        'DL_STATUS'    => $topic_type_gold,
                                        'L_DL_STATUS'    => $l_status,
#
#-----[ FIND ]------------------------------------------
#
// Seeder not seen
foreach ($s_not_seen_opt as $val => $opt)
{
        $selected = ($val == $s_not_seen_val) ? $sl : '';
        $s_not_seen_options .= '<option value="'. $val .'" '. $selected .'>&nbsp;'. $opt['lang'] .'&nbsp;</option>';
}

#
#-----[ AFTER, ADD ]------------------------------------------
#
// Download statuses
foreach ($s_dl_status_opt as $val => $opt)
{
        $selected = ($val == $s_dl_status_val) ? $sl : '';
        $s_dl_status_options .= '<option value="'. $val .'" '. $selected .'>&nbsp;'. $opt['lang'] .'&nbsp;</option>';
}

#
#-----[ FIND ]------------------------------------------
#
	'S_NOT_SEEN_NAME'     => $s_not_seen_key,

#
#-----[ AFTER, ADD ]------------------------------------------
#
        'S_DL_STATUS_NAME'    => $s_dl_status_key,
        'S_DL_STATUS_VAL'     => $s_dl_status_val,

#
#-----[ FIND ]------------------------------------------
#
	'S_NOT_SEEN_OPTIONS' => $s_not_seen_options,

#
#-----[ AFTER, ADD ]------------------------------------------
#
        'S_DL_STATUS_OPTIONS' => $s_dl_status_options,

#
#-----[ OPEN ]------------------------------------------
#
templates/SubSilver/tracker.tpl

#
#-----[ FIND ]------------------------------------------
#
					<td valign="top" class="row1" nowrap="nowrap">
						<fieldset class="fieldset" style="margin: 2px;">
							<legend>{L_SEED_NOT_SEEN}</legend>
							<div style="padding: 4px">
								&nbsp;<select class="post" name="{S_NOT_SEEN_NAME}">{S_NOT_SEEN_OPTIONS}</select>&nbsp;
							</div>
						</fieldset>
					</td>
#
#-----[ AFTER, ADD ]------------------------------------------
#
					<td valign="top" class="row1" nowrap="nowrap">
						<fieldset class="fieldset" style="margin: 2px;">
							<legend> </legend>
							<div style="padding: 4px">
								&nbsp;<select class="post" name="{S_DL_STATUS_NAME}">{S_DL_STATUS_OPTIONS}</select>&nbsp;
							</div>
						</fieldset>
					</td>

#
#-----[ FIND ]------------------------------------------
#
		<th align="center" nowrap="nowrap" title="{L_SIZE}"> {L_SHORT_Size} </th>

#
#-----[ AFTER, ADD ]------------------------------------------
#
		<th align="center" nowrap="nowrap" title="">  </th>

#
#-----[ FIND ]------------------------------------------
#
		<td align="center" title="{tor.TITLE_SPEED}" nowrap="nowrap" class="gensmall">{tor.TOR_SIZE}</td>

#
#-----[ AFTER, ADD ]------------------------------------------
#
		<td align="center" title="{tor.L_DL_STATUS}" class="gensmall">{tor.DL_STATUS}</td>
#
#-----[ OPEN ]------------------------------------------
#
search.php

#
#-----[ FIND ]------------------------------------------
#
if ( $mode == 'searchuser' )

#
#-----[ BEFORE ADD ]------------------------------------------
#
if ( $mode == 'searchgroup' )
{
	//
	// This handles the simple windowed user search functions called from various other scripts
	//

	if ( isset($HTTP_POST_VARS['forma']) || isset($HTTP_GET_VARS['forma']))
	{
		$forma = ( isset($HTTP_POST_VARS['forma']) ) ? $HTTP_POST_VARS['forma'] : $HTTP_GET_VARS['forma'];
		$forma = (in_array($forma, array('group_g'))) ? $forma : '';
	}


	if ( isset($HTTP_POST_VARS['search_username']) )
	{
		groupname_search($HTTP_POST_VARS['search_username'],$forma);
	}
	else
	{
		groupname_search('',$forma);
	}

	exit;
}

#
#-----[ FIND ]------------------------------------------
#
if ( $mode == 'searchuser' )
{
	//
	// This handles the simple windowed user search functions called from various other scripts
	//
	if ( isset($HTTP_POST_VARS['search_username']) )
	{
		username_search($HTTP_POST_VARS['search_username']);
	}
	else
	{
		username_search('');
	}

	exit;
}
#
#-----[ REPLACE WITH ]------------------------------------------
#
if ( $mode == 'searchuser' )
{
	//
	// This handles the simple windowed user search functions called from various other scripts
	//
	if ( isset($HTTP_POST_VARS['forma']) || isset($HTTP_GET_VARS['forma']))
	{
		$forma = ( isset($HTTP_POST_VARS['forma']) ) ? $HTTP_POST_VARS['forma'] : $HTTP_GET_VARS['forma'];
		$forma = (in_array($forma, array('username_pg', 'group_g', 'username_png'))) ? $forma : '';
	}

	if ( isset($HTTP_POST_VARS['search_username']) )
	{
		username_search($HTTP_POST_VARS['search_username'], $forma);
	}
	else
	{
		username_search('', $forma);
	}

	exit;
}

#
#-----[ OPEN ]------------------------------------------
#
includes/functions_search.php

#
#-----[ FIND ]------------------------------------------
#
//
// Username search
//
#
#-----[ BEORE ADD ]------------------------------------------
#
//
// Groupname search
//
function groupname_search($search_match, $forma)
{
	global $db, $board_config, $template, $lang, $images, $theme, $phpEx, $phpbb_root_path;
	global $starttime, $gen_simple_header;
	
	$gen_simple_header = TRUE;

	$groupname_list = '';
	if ( !empty($search_match) )
	{
		$groupname_search = preg_replace('/\*/', '%', phpbb_clean_username($search_match));

		$sql = "SELECT group_name 
			FROM " . GROUPS_TABLE . " 
			WHERE group_name LIKE '" . str_replace("\'", "''", $groupname_search) . "' AND 'group_name' <> ''
			ORDER BY group_name";
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
		}

		if ( $row = $db->sql_fetchrow($result) )
		{
			do
			{
				$groupname_list .= '<option value="' . $row['group_name'] . '">' . $row['group_name'] . '</option>';
			}
			while ( $row = $db->sql_fetchrow($result) );
		}
		else
		{
			$groupname_list .= '<option>' . $lang['No_match']. '</option>';
		}
		$db->sql_freeresult($result);
	}

	$page_title = $lang['Search'];
	include($phpbb_root_path . 'includes/page_header.'.$phpEx);

	$template->set_filenames(array(
		'search_user_body' => 'search_username.tpl')
	);

	$template->assign_vars(array(
		'USERNAME' => (!empty($search_match)) ? phpbb_clean_username($search_match) : '', 
		'USERNAME_PG' => (!empty($search_match)) ? phpbb_clean_username($search_match) : '', 


		'L_CLOSE_WINDOW' => $lang['Close_window'], 
		'L_SEARCH_USERNAME' => ' ', 
		'L_UPDATE_USERNAME' => $lang['Select_username'], 
		'L_SELECT' => $lang['Select'], 
		'L_SEARCH' => $lang['Search'], 
		'L_SEARCH_EXPLAIN' => $lang['Search_author_explain'], 
		'L_CLOSE_WINDOW' => $lang['Close_window'], 
		'FORMA' => ($forma) ? $forma:'username', 

		'S_USERNAME_OPTIONS' => $groupname_list, 
		'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=searchgroup&forma=$forma"))
	);

	if ( $groupname_list != '' )
	{
		$template->assign_block_vars('switch_select_name', array());
	}

	$template->pparse('search_user_body');

	include($phpbb_root_path . 'includes/page_tail.'.$phpEx);

	return;
}
#
#-----[ FIND ]------------------------------------------
#
function username_search($search_match)

#
#-----[ REPLACE WITH ]------------------------------------------
#
function username_search($search_match, $forma)

#
#-----[ FIND ]------------------------------------------
#
		'L_CLOSE_WINDOW' => $lang['Close_window'], 
#
#-----[ AFTER ADD ]------------------------------------------
#
		'FORMA' => ($forma) ? $forma:'username', 
#
#-----[ FIND ]------------------------------------------
#
		'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=searchuser"))

#
#-----[ REPLACE WITH ]------------------------------------------
#
		'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=searchuser&forma=$forma"))
#
#-----[ OPEN ]------------------------------------------
#
templates/SubSilver/search_username.tpl

#
#-----[ FIND ]------------------------------------------
#
	opener.document.forms['post'].username.value = selected_username;

#
#-----[ REPLACE WITH ]------------------------------------------
#
	opener.document.forms['post'].{FORMA}.value = selected_username;
#
#-----[ OPEN ]------------------------------------------
#
includes/auth.php

#
#-----[ FIND ]------------------------------------------
#
		$auth_user['auth_mod'] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;

#
#-----[ AFTER, ADD ]------------------------------------------
#
		$auth_user['auth_reg'] = ($userdata['session_logged_in'] ) ? TRUE : 0;
		$auth_user['auth_admin'] = ($is_admin ) ? true : 0;
#
#-----[ OPEN ]------------------------------------------
#
groupcp.php

#
#-----[ FIND ]------------------------------------------
#

					$sql = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)
						VALUES (" . $row['user_id'] . ", $group_id, 0)";
					if ( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, 'Could not add user to group', '', __LINE__, __FILE__, $sql);
					}
#
#-----[ AFTER ADD ]------------------------------------------
#
					$sql = "SELECT prevs_id
						FROM " . DOWNLOAD_STATUSES_TBL . "
						WHERE prevs_group_g=$group_id";
					if ( !($result = $db->sql_query($sql)) )
					{
						message_die(GENERAL_ERROR, 'Could not get user information', '', __LINE__, __FILE__, $sql);
					}
					if ($db->sql_fetchrow($result))
						{
							$user_id_g= $row['user_id'];

							$this_userdata_g = get_userdata($user_id_g);
							$r_total_q = ($group_info['combination_g']=='0') ? ($group_info['r_total_q']) : (($this_userdata_g['r_total_q']=='-1') ? '-1': ($group_info['r_total_q'] =='-1') ? '-1': 'r_total_q+'.$group_info['r_total_q']);
							$r_per_p = $group_info['r_per_p'];
							$r_period_t = $group_info['r_period_t'];


							$sql = "UPDATE " . USERS_TABLE . " SET r_total_q=$r_total_q, r_per_p=$r_per_p, r_period_t=$r_period_t, r_cur_c=0, r_next_t=0 WHERE user_id=$user_id_g";
							if ( !($result = $db->sql_query($sql)) )
							{
								message_die(GENERAL_ERROR, "Couldn't insert restriction data into users table", "", __LINE__, __FILE__, $sql);
							}
						}
#
#-----[ FIND ]------------------------------------------
#
						$sql = "UPDATE " . USER_GROUP_TABLE . " 
							SET user_pending = 0 
							WHERE user_id IN ($sql_in) 
								AND group_id = $group_id"; 

#
#-----[ AFTER ADD ]------------------------------------------
#
						$sql_a = "SELECT prevs_id
							FROM " . DOWNLOAD_STATUSES_TBL . "
							WHERE prevs_group_g=$group_id";
						if ( !($result_a = $db->sql_query($sql_a)) )
						{
							message_die(GENERAL_ERROR, 'Could not get user information', '', __LINE__, __FILE__, $sql);
						}
						if ($db->sql_fetchrow($result_a))
							{
								for($i = 0; $i < count($members); $i++)
								{
					
									$user_id_g= $members[$i];
						
									$this_userdata_g = get_userdata($user_id_g);
									$r_total_q = ($group_info['combination_g']=='0') ? ($group_info['r_total_q']) : (($this_userdata_g['r_total_q']=='-1') ? '-1': ($group_info['r_total_q'] =='-1') ? '-1': 'r_total_q+'.$group_info['r_total_q']);
									$r_per_p = $group_info['r_per_p'];
									$r_period_t = $group_info['r_period_t'];
						
						
									$sql_b = "UPDATE " . USERS_TABLE . " SET r_total_q=$r_total_q, r_per_p=$r_per_p, r_period_t=$r_period_t, r_cur_c=0, r_next_t=0 WHERE user_id=$user_id_g";
									if ( !($result_b = $db->sql_query($sql_b)) )
									{
										message_die(GENERAL_ERROR, "Couldn't insert restriction data into users table", "", __LINE__, __FILE__, $sql);
									}
								}
					
							}

# 
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------ 
# 
# EoM